//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
Page({
    data: {
        imgUrls: [
            'https://img.mukewang.com/szimg/59b8a4910001cb0509000500.jpg',
            'https://img.mukewang.com/szimg/59b8a4910001cb0509000500.jpg',
            'https://img.mukewang.com/szimg/59b8a4910001cb0509000500.jpg'
        ],
        indicatorDots: false,
        autoplay: false,
        interval: 5000,
        duration: 1000,
        systemInfo: app.systemInfo,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        list: [
            { 'name': '数学', 'src': '../../image/icon/jingdianwenxue.png', 'id': '1' },
            { 'name': '语文', 'src': '../../image/icon/kecheng.png', 'id': '2' },
            { 'name': '英语', 'src': '../../image/icon/shuxue.png', 'id': '3' },
            { 'name': '作文', 'src': '../../image/icon/yingyu.png', 'id': '4' },
            { 'name': '作文', 'src': '../../image/icon/yuwen.png', 'id': '5' },
        ],
        bannerHeight: "200",
        workList: [
            { 'name': 'windows图片查看器官方版软件下载图片查看器免费正式版', 'src': '../../image/timg.jpg', 'datatime': '2019-01-29 18:45:27', 'type': '会员文章', 'id': '1' },
            { 'name': 'windows图片查看器官方版软件下载图片查看器免费正式版', 'src': '../../image/timg.jpg', 'datatime': '2019-01-29 18:45:27', 'type': '会员文章', 'id': '1' },
            { 'name': 'windows图片查看器官方版软件下载图片查看器免费正式版', 'src': '../../image/timg.jpg', 'datatime': '2019-01-29 18:45:27', 'type': '会员文章', 'id': '1' },
            { 'name': 'windows图片查看器官方版软件下载图片查看器免费正式版', 'src': '../../image/timg.jpg', 'datatime': '2019-01-29 18:45:27', 'type': '会员文章', 'id': '1' },
            { 'name': 'windows图片查看器官方版软件下载图片查看器免费正式版', 'src': '../../image/timg.jpg', 'datatime': '2019-01-29 18:45:27', 'type': '会员文章', 'id': '1' },
        ],
        advert: null
    },
    imgs: function(e) { //单广告位图片高度
        util.imgHeight(e, (data) => {
            var viewHeight = (this.data.systemInfo.screenWidth - 20) / data;
            this.setData({
                bannerHeight: viewHeight
            })
        })
    },
    goto: function(e) {
        let pageId = e.currentTarget.dataset.id
        console.log(e.detail.userInfo)
        let data = {
            source: '3',
            extension_code: '',
            question_option_code: '',
            question_option_describe: '',
            telphone: '',
            agent_id: '',
            name: e.detail.userInfo.nickName,
            nickname: e.detail.userInfo.nickName,
            sex: e.detail.userInfo.gender,
            province: e.detail.userInfo.province,
            city: e.detail.userInfo.city,
            country: e.detail.userInfo.country,
            headimgurl: e.detail.userInfo.avatarUrl,
            privilege: '',
            unionid: '',
            openId: app.openId
        }
        app.userInfo = e.detail.userInfo
        util.postRequest('FinanceManage/rest/wxLogin.do', { openId: app.openId }, function(res) {
            console.log(res)
            if (res.errorcode == 1) {
                console.log("yes")
                wx.switchTab({
                    url: '/pages/index/index'
                });
            } else {
                console.log("no")

                util.postRequest('FinanceManage/rest/registerMember.do', {}, function(res) {
                    console.log(res)
                    if (res.errorcode == 1) {
                        console.log("yesreg")

                        wx.switchTab({
                            url: '/pages/index/index'
                        });
                    }
                })
            }
        })
    },
    onLoad: function() {
        let _self = this
        wx.cloud.init()
        wx.cloud.callFunction({
            name: 'getUserInfo',
        }).then(
            ress => {
                console.log(ress)
                app.openId = ress.result.openId
                util.postRequest('FinanceManage/rest/wxLogin.do', { openId: app.openId }, function(res) {
                    console.log(res)
                    if (res.errorcode == 1) {
                        console.log("yesreg")
                        wx.getSetting({
                            success: res => {
                                console.log(res)
                                if (res.authSetting['scope.userInfo']) {
                                    console.log("asdf")
                                        // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                                    wx.getUserInfo({
                                        success: res => {
                                            console.log(res)
                                                // 可以将 res 发送给后台解码出 unionId
                                            app.userInfo = res.userInfo
                                            wx.switchTab({
                                                    url: '/pages/index/index'
                                                })
                                                // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                                                // 所以此处加入 callback 以防止这种情况
                                        }
                                    })
                                }

                            }
                        })
                    }
                })
            }
        )




    },

})